import { EventService, EventSource } from './../../services/event.service';
import { RequestReplyService } from 'src/app/services/request-reply.service';
import { RegistModel, UserType } from './../../entity/TT';
import { FormControl, NgForm, Validators } from '@angular/forms';
import { MatDialogRef } from '@angular/material/dialog';
import { Component, OnInit } from '@angular/core';
import { NGXLogger } from 'ngx-logger';



@Component({
  selector: 'app-register-dialog',
  templateUrl: './register-dialog.component.html',
  styleUrls: ['./register-dialog.component.scss']
})
export class RegisterDialogComponent implements OnInit {

  // invite  = '';
  // phone = '';
  // password = '';
  // userType = 'Member';

  model = new RegistModel('', '',  '', null)

  msg = '';

  inviteControl = new FormControl(null, Validators.required);
  phoneControl = new FormControl(null, Validators.required);
  passwordControl = new FormControl(null, Validators.required);

  constructor(
    private log: NGXLogger,
    public dialogRef: MatDialogRef<RegisterDialogComponent>,
    private requestService: RequestReplyService,
    private eventService: EventService
  ) {}

  ngOnInit() {

  }

  doClose(): void {
    this.dialogRef.close();
  }

  doClearMsg(): void {
    this.msg = null;
  }

  doSubmit(f: NgForm) {
    this.doClearMsg();
    // this.log.debug('submit', this.invite, this.phone, this.password, this.userType);
    const regisertDTO = this.model;
    this.requestService.Register(regisertDTO).subscribe(r => {
      this.log.debug('register', r);
      this.msg = r.msg;
      if (r.success) {
        this.eventService.sendEvent(EventSource.REGISTER_EVENT, regisertDTO);
        this.doClose();
      }
    });
  }

}
